﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAO;

namespace Sklad.WindowsApplication
{
    public partial class SendForm : Form
    {
        public static DaoRepository dr = new DaoRepository();
        public static BindingSource bs = new BindingSource();

        public SendForm()
        {
            InitializeComponent();
            this.bindingSource_sending.DataSource = new BindingList<Extradition>(dr.ExtraditionRepository.GetAll().ToList());
            this.bindingNavigator_sending.BindingSource = this.bindingSource_sending;
            LinkElements();
        }

        private void LinkElements()
        {
            this.bindingNavigator_sending.BindingSource = this.bindingSource_sending;
            this.tb_send_id.DataBindings.Add("text", this.bindingSource_sending, "Id");
            this.dp_send.DataBindings.Add("text", this.bindingSource_sending, "Date");
            this.tb_send_empl.DataBindings.Add("text", this.bindingSource_sending, "Employer");
            this.tb_client.DataBindings.Add("text", this.bindingSource_sending, "Client");
            this.dataGridView1.DataSource = bs;
            UpdateDataGrid();
            this.bindingSource_sending.CurrentChanged += bindingSource_recieve_CurrentChanged;
        }

        private void bindingSource_recieve_CurrentChanged(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }

        private void UpdateData()
        {
            this.bindingSource_sending.DataSource = new BindingList<Extradition>(dr.ExtraditionRepository.GetAll().ToList());
        }

        void UpdateDataGrid()
        {
            var temp = this.bindingSource_sending.Current as Extradition;
            if (temp == null)
            {
                return;
            }
            var templist = from Extradition_Product in temp.Extradition_Product
                           select new
                           {
                               Extradition_Product.Product.Name,
                               Extradition_Product.Count
                           };

            bs.DataSource = templist;
        }


        private void bt_add_client_Click(object sender, EventArgs e)
        {
            AddClientForm apf = new AddClientForm();
            apf.Visible = true;
            apf.FormClosing += apf_FormClosing;
            this.Visible = false;
        }

        private void apf_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = true;
        }

        private void bt_add_sending_Click(object sender, EventArgs e)
        {
            AddSendingForm adf = new AddSendingForm();
            adf.Visible = true;
            adf.FormClosing += adf_FormClosing;
            this.Visible = false;
        }

        private void adf_FormClosing(object sender, FormClosingEventArgs e)
        {
            AddSendingForm.itemlist = null;
            UpdateData();
            UpdateDataGrid();
            this.Visible = true;
        }
    }
}
